const express = require("express");
const router = express.Router();
//导入数据库的视图
const Rent = require("../../models/Rent");
const { URL } = require("../../config/keys");
// 租赁创建
router.post("/create", (req, res) => {
  Rent.find({
    user_id: req.body.user_id,
    end_data: null,
  }).then((data) => {
    if (data.length > 0) {
      res.json({
        success: false,
        msg: "请还车后再进行租赁",
      });
    } else {
      const rent = new Rent({
        bike_id: req.body.bike_id,
        user_id: req.body.user_id,
      });
      rent
        .save()
        .then((user) =>
          res.json({
            success: true,
            msg: "租赁成功",
          })
        )
        .catch((err) => console.log(err));
    }
  });
});

router.get("/getList", (req, res) => {
  Rent.aggregate(
    [
      {
        $lookup: {
          from: "bikes",
          localField: "bike_id",
          foreignField: "_id",
          as: "bikes",
        },
      },
      {
        $lookup: {
          from: "users",
          localField: "user_id",
          foreignField: "_id",
          as: "users",
        },
      },
      {
        //展示的数据
        $project: {
          _id: 1,
          end_data: 1,
          start_date: 1,
          bikes: 1,
          users: 1,
        },
      },
      {
        $unwind: {
          // 拆分子数组
          path: "$users",
          preserveNullAndEmptyArrays: true, // 空的数组也拆分
        },
      },
      {
        $unwind: {
          // 拆分子数组
          path: "$bikes",
          preserveNullAndEmptyArrays: true, // 空的数组也拆分
        },
      },
    ],
    (err, docs) => {
      if (err) {
        return console.log(err);
      }
      // docs.forEach((element) => {
      //   element.users.avatar = URL + element.user.avatar;
      // });
      res.json({
        success: true,
        result: docs,
      });
    }
  );
});

router.get("/deleteRent/:id", (req, res) => {
  Rent.remove({ _id: req.params.id }).then((type) => {
    res.json({
      success: true,
      msg: "删除成功",
    });
  });
});

router.get("/updateRent/:id", (req, res) => {
  console.log(req.params.id);
  Rent.update({ _id: req.params.id }, { $set: { end_data: Date.now() } }).then(
    (type) => {
      res.json({
        success: true,
        msg: "还车成功",
      });
    }
  );
});

router.get("/returnList", (req, res) => {
  // Rent.find({
  //   end_data: { $exists: true },
  // }).then((data) => {
  //   res.json({
  //     success: true,
  //     result: data,
  //   });
  Rent.aggregate(
    [
      {
        $lookup: {
          from: "bikes",
          localField: "bike_id",
          foreignField: "_id",
          as: "bikes",
        },
      },
      {
        $lookup: {
          from: "users",
          localField: "user_id",
          foreignField: "_id",
          as: "users",
        },
      },
      {
        //展示的数据
        $project: {
          _id: 1,
          end_data: 1,
          start_date: 1,
          bikes: 1,
          users: 1,
        },
      },
      {
        $unwind: {
          // 拆分子数组
          path: "$users",
          preserveNullAndEmptyArrays: true, // 空的数组也拆分
        },
      },
      {
        $unwind: {
          // 拆分子数组
          path: "$bikes",
          preserveNullAndEmptyArrays: true, // 空的数组也拆分
        },
      },
      {
        $match: {
          end_data: { $exists: true },
        },
      },
    ],
    (err, docs) => {
      if (err) {
        return console.log(err);
      }
      // docs.forEach((element) => {
      //   element.users.avatar = URL + element.user.avatar;
      // });
      res.json({
        success: true,
        result: docs,
      });
    }
  );
});

// 我的订单
router.get("/myOrder/:id", (req, res) => {
  console.log("请求了", req.params.id);
  Rent.aggregate(
    [
      {
        $lookup: {
          from: "bikes",
          localField: "bike_id",
          foreignField: "_id",
          as: "bikes",
        },
      },
      {
        //展示的数据
        $project: {
          _id: 1,
          end_data: 1,
          start_date: 1,
          bikes: 1,
        },
      },
      {
        $unwind: {
          // 拆分子数组
          path: "$users",
          preserveNullAndEmptyArrays: true, // 空的数组也拆分
        },
      },
      {
        $unwind: {
          // 拆分子数组
          path: "$bikes",
          preserveNullAndEmptyArrays: true, // 空的数组也拆分
        },
      },
      {
        $match: {
          user_id: req.param.id,
        },
      },
    ],
    (err, docs) => {
      if (err) {
        return console.log(err);
      }
      let data = []
      docs.map((element) => {
        element = { ...element, ...element.bikes, _id: element._id };
        delete element.bikes;
        if (element.name) {
          data.push(element)
        }

      });
      console.log(data)
      res.json({
        success: true,
        result: data,
      });
    }
  );
});


router.get("/searchList/:name", (req, res) => {
  console.log(req.params.name)
  Rent.aggregate(
    [
      {
        $match: {
          name: req.params.name,
        },
      },
      {
        $lookup: {
          from: "bikes",
          localField: "bike_id",
          foreignField: "_id",
          as: "bikes",
        },
      },
      {
        $lookup: {
          from: "users",
          localField: "user_id",
          foreignField: "_id",
          as: "users",
        },
      },
      {
        //展示的数据
        $project: {
          _id: 1,
          end_data: 1,
          start_date: 1,
          bikes: 1,
          users: 1,
        },
      },
      {
        $unwind: {
          // 拆分子数组
          path: "$users",
          preserveNullAndEmptyArrays: true, // 空的数组也拆分
        },
      },
      {
        $unwind: {
          // 拆分子数组
          path: "$bikes",
          preserveNullAndEmptyArrays: true, // 空的数组也拆分
        },
      },
    ],
    (err, docs) => {
      if (err) {
        return console.log(err);
      }
      // docs.forEach((element) => {
      //   element.users.avatar = URL + element.user.avatar;
      // });
      res.json({
        success: true,
        result: docs,
      });
    }
  );
});


module.exports = router;
